package com.eascs.spider.huangye88.manager;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;

import com.eascs.spider.huangye88.actor.Huangye88Actor;
import com.eascs.spider.huangye88.util.NetUtil;
/**
 * 
 * @author hzp
 *
 */
public class CrawlerGuangdongManager {
	
	public static Queue<String> queue = new ConcurrentLinkedQueue<>();
	private static ExecutorService executors;
	private static CrawlerGuangdongManager instance;
	public static CloseableHttpClient client;
	
	
	public static CrawlerGuangdongManager init(){
		executors = Executors.newFixedThreadPool(100);
		BasicCookieStore cookieStore = new BasicCookieStore();
		client = NetUtil.initHttpClient(cookieStore);
		Schdule schdule = new Schdule();
		schdule.start();
		instance = new CrawlerGuangdongManager();
		return instance;
	}
	
	public static CrawlerGuangdongManager getInstance(){
		return init();
	}
	
	//添加请求url到队列
	public void addUrl(String url){
		queue.add(url);
	}
	
	static class Schdule extends Thread{
		
		@Override
		public void run(){
			while(true){
				try {
					if(!queue.isEmpty()){
						Huangye88Actor actor = new Huangye88Actor(client, queue.poll());
						executors.execute(actor);
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
}